<?php 
namespace App\Models;
use Log;
use App\Cache\BaseCache;

class BookRecord extends BaseModel{
    
	//表明
	protected $table = 'book_record';
	//主键
	protected $primaryKey = 'id';
	
    public function addBook(){
        $userId = $this->request->input('userId');
        $liveId = $this->request->input('liveId');
        $book = $this->select('id','user_id','live_id')
                ->where('user_id','=',$userId)
                ->where('live_id','=',$liveId)
                ->where('is_delete','=',0)
                ->first();
        if($book){
            return TRUE;
        }
        $book = new BookRecord($this->request);
        $book->user_id = $userId;
        $book->live_id = $liveId;
        $book->status = 0;
        $book->save();
        return TRUE;
    }
    
    public function delBook(){
        $userId = $this->request->input('userId');
        $liveId = $this->request->input('liveId');
        $book = $this->select('id','user_id','live_id')
                ->where('user_id','=',$userId)
                ->where('live_id','=',$liveId)
                ->first();
        if($book){
            $book->is_delete = 1;
            $book->save();
        }
        return TRUE;
    }
    
    //判断是否预定
    public function isBook($userId,$liveId){
        $book = $this->select('id','user_id','live_id')
                ->where('user_id','=',$userId)
                ->where('live_id','=',$liveId)
                ->where('is_delete','=','0')
                ->first();
        if(!$book){
           $book = new BookRecord($this->request);
           $book->user_id = $userId;
           $book->live_id = $liveId;
           $book->is_delete = 1;
        }
        $book->saveModel();
        return !$book->is_delete ;
    }
    
    
    //更新实体
    public function saveModel(){
        $cache = new BaseCache();
        $cache->method = 'forever';
        $cache->cacheName = 'book';
        $cache->cachePar = array($this->user_id,$this->live_id); //集合
        if($this->is_delete == 0){
            $cache->cacheValue = 'BOOK';
        } else {
            $cache->method = 'put';
            $cache->cacheValue = 'UNBOOK';
        }
        $cache->HandleCache();
    }
    
    //删除实体
    public function deleteModel(){
        $cache = new BaseCache();
        $cache->method = 'forever';
        $cache->cacheName = 'book';
        $cache->cachePar = array($this->user_id,$this->live_id); //集合
        $cache->cacheValue = 'UNBOOK';
        $cache->HandleCache();
    }
    
}